/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * janela.java
 *
 * Created on 27/04/2009, 20:59:14
 */
package bancodedados3d;

import java.util.Enumeration;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author fosa
 */
public class janela extends javax.swing.JFrame {

    /** Creates new form janela */
    public janela() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jDesktopPane1 = new javax.swing.JDesktopPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Gerenciamento de Banco de Dados 3D");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jToolBar1.setRollover(true);

        jButton1.setText("novo");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 704, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTree1.setModel(null);
        jTree1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTree1MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTree1);

        jDesktopPane1.setBackground(new java.awt.Color(98, 98, 224));
        jDesktopPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Desktop"));
        jDesktopPane1.setForeground(java.awt.Color.white);
        jDesktopPane1.setAutoscrolls(true);

        jMenu1.setText("File");
        jMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        jMenuItem1.setText("Nova janela");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Conectar");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        jMenu3.setText("Formatar");
        jMenu2.add(jMenu3);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 588, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu1ActionPerformed
        //aff
    }//GEN-LAST:event_jMenu1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.jDesktopPane1.add(new FrameInterno3d02("Janela de Consulta", 10, 10));
        setJtree();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        jDesktopPane1.add(new FrameInterno3d02("Janela de consulta", 0, 0));
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
//        Login.main(new String[1]);
        Main.GlLogin.show();
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jTree1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTree1MouseClicked
        // TODO add your handling code here:
//        System.out.println(this.jTree1.getLeadSelectionPath().getPath());
        Enumeration arvore;
        System.out.println(evt.getClickCount());

        switch (evt.getClickCount()) {
            case 1:
                if (this.jTree1.getLeadSelectionPath().getPathCount() == 2) {

                    Object[][][] tabela = Main.GlConsulta.consulta("show tables from " + this.jTree1.getLeadSelectionPath().getLastPathComponent().toString());


                    DefaultMutableTreeNode treeNodeRoot = (DefaultMutableTreeNode) this.jTree1.getModel().getRoot();

                    arvore = treeNodeRoot.children();

                    try {
                        int cont = 1;
                        while (cont < treeNodeRoot.getChildCount()) {
                            this.jTree1.collapseRow(cont);
                            cont++;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    int nodeindex = this.jTree1.getLeadSelectionRow();
                    for (int i = 1; i < nodeindex; i++) {
                        arvore.nextElement();
                    }
                    DefaultMutableTreeNode treeNodeSelected = (DefaultMutableTreeNode) arvore.nextElement();

                    System.out.println(treeNodeSelected.toString());

                    if (tabela != null) {
                        try {
                            DefaultMutableTreeNode[] treeNodeNovo = new DefaultMutableTreeNode[tabela[1].length];
                            for (int i = 0; i < tabela[1].length; i++) {
                                treeNodeNovo[i] = new javax.swing.tree.DefaultMutableTreeNode(tabela[1][i][0].toString());
                                treeNodeSelected.add(treeNodeNovo[i]);
                            }
                        } catch (NullPointerException nu) {
                            nu.printStackTrace();
                        }
                    }
                }
                break;
            case 2:
                if (this.jTree1.getLeadSelectionPath().getPathCount() == 3) {
                    this.jDesktopPane1.add(new FrameInterno3d02("Janela de Consulta", "select * from " + this.jTree1.getLeadSelectionPath().getPath()[1] + "." + this.jTree1.getLeadSelectionPath().getLastPathComponent().toString(), 10, 10));
                }
                this.jTree1.expandRow(this.jTree1.getLeadSelectionRow());
                break;
        }
    }//GEN-LAST:event_jTree1MouseClicked

    public void setJtree() {
        jTree1.setModel(null);
        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("Bancos de Dados");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = null;

        Object[][][] bancos = Main.GlConsulta.consulta("show databases");
        for (int i = 0; i < bancos[1].length; i++) {
            treeNode2 = new javax.swing.tree.DefaultMutableTreeNode(bancos[1][i][0].toString());
            System.out.println(bancos[1][i][0]);
            treeNode1.add(treeNode2);
        }
        this.jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        this.jScrollPane2.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new janela().setVisible(true);
            }
        });
    }
    private Login janelalogin;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables
}

